package com.zp.self.module.编程测试;

import java.util.HashMap;

/**
 * @author By ZengPeng
 * @Description
 * @date in  2021/4/9 16:40
 * @Modified By
 */
public class 数组中求两数之和_为目标值 {

    @org.junit.Test
    public void test(){
        int[] arr = new int[]{3, 2, 1, 0, 7, 4, -6, 5, 9};
        int target = 5;
        jiShuan(arr,target);

    }

    private void jiShuan(int[] arr, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            if(map.containsKey(arr[i])){
                System.out.println("a["+map.get(arr[i])+"] + "+"a["+i+"]" + " = " +target);
            }
            map.put(target - arr[i],i);
        }
    }
}
//a[0] + a[1] = 5
//a[2] + a[5] = 5
//a[3] + a[7] = 5